/**
 * @(#) ApprovalDetailProxy.as
 *
 * @date		2010. 09. 08.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @client		Network O&S 
 * @description	결재 상세내역   Server Connection Proxy (결재함)
 * 				approval.ApprovalDetailProxy에서 복사..
 */
package com.skt.common_hr.approval_nos.model
{
	import com.skt.common_hr.approval_nos.model.vo.ApprovalDetailVO;
	import com.skt.common_hr.approval_nos.model.vo.ApprovalLineVO;
	import com.skt.common_hr.approval_nos.model.vo.FileVO;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class ApprovalDetailProxy extends CommonProxy
	{
		public static const NAME:String = "ApprovalDetailProxy";
		
		public static const LOAD_APPROVAL_DETAIL_COMPLETE:String 	= "loadApprovalDetailComplete";
		public static const LOAD_APPROVAL_DETAIL_FAILED:String 		= "loadApprovalDetailFailed";
		
		public var fileList:ArrayCollection;
		public var lineList:ArrayCollection;
		public var detailInfo:ApprovalDetailVO;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ApprovalDetailProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadDetail(key:String):void
		{
			var params:Object = new Object;
			params["I_REQNR"] = key;
			
			loadData("Z_HR_0107", params, resultLoad, faultLoad);
		}
		
		private function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {
				if(event.result.RESULT1 is Array) 
					fileList = parseAttatchedFiles(new ArrayCollection(event.result.RESULT1));
				else 
					fileList = parseAttatchedFiles(event.result.RESULT1);					
				if(event.result.RESULT2 is Array) 
					lineList = parseAppLines(new ArrayCollection(event.result.RESULT2));
				else 
					lineList = parseAppLines(event.result.RESULT2);
				if(event.result.RESULT3 is Array) 
					detailInfo = parseDetailInfo(new ArrayCollection(event.result.RESULT3));
				else 
					detailInfo = parseDetailInfo(event.result.RESULT3);
				if(detailInfo!=null) {
					detailInfo.CONTENT = event.result["O_CONTENT"];
				}
				facade.sendNotification(LOAD_APPROVAL_DETAIL_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_APPROVAL_DETAIL_FAILED, this);
			}
		}
		
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_APPROVAL_DETAIL_FAILED, this);
		}
		
		
		private function parseAttatchedFiles(src:ArrayCollection):ArrayCollection
		{
			var result:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:FileVO = new FileVO;
				vo.SEQNO 		= rec.SEQNO;
				vo.FILE_NAME	= rec.FILE_NAME;
				vo.FILE_URL 	= rec.FILE_URL;
				vo.FILE_EXT 	= rec.FILE_EXT;
				vo.FILE_SIZE 	= rec.FILE_SIZE;
				result.addItem(vo);
			}
			return result;			
		}
		
		private function parseAppLines(src:ArrayCollection):ArrayCollection
		{
			var result:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:ApprovalLineVO = new ApprovalLineVO;
				vo.GWARELEV 	= rec.GWARELEV;
				vo.ENAME		= rec.ENAME;
				vo.WPERNR 		= rec.WPERNR;
				vo.ORGTX 		= rec.ORGTX;
				vo.PLANST 		= rec.PLANST;
				vo.GWARE_INDATE = DateUtil.stringToDateTime(rec.GWARE_INDATE);
				vo.WSTATUS 		= rec.WSTATUS;
				vo.WSTATUS_TX	= rec.WSTATUS_TX;
				result.addItem(vo);
			}
			return result;			
		}
		
		
		private function parseDetailInfo(src:ArrayCollection):ApprovalDetailVO
		{
			if(src.length>0) {
				var vo:ApprovalDetailVO = new ApprovalDetailVO();
				var rec:Object = src.getItemAt(0);
				vo.PROCGBN 	= rec.PROCGBN;
				vo.TITLE 	= rec.TITLE;
				vo.GWARELEV = rec.GWARELEV;
				vo.WSTATUS 	= rec.WSTATUS;
				//vo.CONTENT 	= rec.CONTENT;
				vo.CMTXT 	= rec.CMTXT;
				return vo;
			}
			return null;
		}
	}
}